package com.springcloud.feigndemoserver1000.controller;

import com.springcloud.common.dto.PageDTO;
import com.springcloud.common.dto.ResponseData;
import com.springcloud.common.dto.UserDTO;
import com.springcloud.common.enums.ErrorCodeEnum;
import com.springcloud.feigndemoserver1000.service.FeignUserService;
import feign.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private FeignUserService feignUserService;

    /*
     * @Author ZhangBin
     * @Date 2020/6/30 13:53
     * @Description: 根据id获取用户信息
     *
     **/
    @GetMapping(value = "/getUserById/{userId}")
    public ResponseData<UserDTO> getUserById(@PathVariable(name="userId")Integer id,HttpServletRequest request){
        log.info("token={}",request.getHeader("token"));
        log.info("userId={}",id);
        log.debug("userId={}",id);
        log.error("userId={}",id);
        ResponseData<UserDTO> responseData = feignUserService.getUserById(id);
        return responseData;
    }
    /*
     * @Author ZhangBin
     * @Date 2020/6/30 13:53
     * @Description: 分页查询
     *
     **/
    @GetMapping(value ="/getUserPage")
    public ResponseData<PageDTO<UserDTO>> getUserPage(PageDTO pageDTO){
        ResponseData<PageDTO<UserDTO>> responseData =  feignUserService.getUserPage(pageDTO);
        return responseData;
    }

    /*
     * @Author ZhangBin
     * @Date 2020/8/19 16:41
     * @Description: feign-form文件上传测试
     *  页面input框需要有name=file属性： <input type="file" name="file">，需要跟@RequestParam(value = "file")的value值对应
     **/
    @RequestMapping(value = "/fileUploadTest",method = RequestMethod.POST)
    public ResponseData<?> fileUploadTest(@RequestPart(value = "file") MultipartFile file,String remark){
        log.info("开始文件上传");
        if(file == null || file.getSize()<=0){
            log.info("文件大小不满足要求");
            return ResponseData.response(ErrorCodeEnum.FAIL,null);
        }
        ResponseData<?> responseData = feignUserService.fileUploadTest(file,remark);
        log.info("文件上传结束");
        return responseData;
    }
    /*
     * @Author ZhangBin
     * @Date 2020/8/20 17:21
     * @Description: Feign返回图片流测试，如：图形验证码
     *
     **/
    @RequestMapping("/imgIOStreamTest")
    public void imgIOStreamTest(HttpServletResponse httpServletResponse){
        log.info("开始获取图片");
        Response feignResponse = feignUserService.imgIOStreamTest();
        Response.Body body = feignResponse.body();
        InputStream fileInputStream = null;
        OutputStream outStream = null;
        try {
            fileInputStream = body.asInputStream();
            outStream = httpServletResponse.getOutputStream();
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fileInputStream.read(bytes)) != -1) {
                outStream.write(bytes, 0, len);
            }
            outStream.flush();
            log.info("获取图片结束");
        } catch (Exception e) {
            log.error("获取图片流异常",e);
        }finally {
            try{
                if(outStream != null)outStream.close();
                if(fileInputStream != null)fileInputStream.close();
            }catch (Exception e){

            }
        }
    }


}
